package com.hy.item.service;

import com.hy.common.enums.ExceptionEnums;
import com.hy.common.exception.XkException;
import com.hy.item.mapper.CategoryMapper;
import com.hy.item.pojo.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class CategoryService {
    //注入mapper
    @Autowired
    private CategoryMapper categoryMapper;

    //通过pid查询分类列表
    public List<Category> queryCategoryListByPid(Long pid) {

        Category category = new Category();
        //将字段设置到对象中
        category.setParentId(pid);
        List<Category> list=categoryMapper.select(category);
        //判断集合是否为空CollectionUtils.isEmpty(list)相当于list==null||list.isEmpty()
        if(CollectionUtils.isEmpty(list)){
            throw new XkException(ExceptionEnums.CATEGORY_NOT_FOUND);
        }
        return list;
    }
    //根据goodsService中传递过来的spu表中cid查询商品分类名称
    public List<Category> queryByIds(List<Long> ids){//通过一堆id查询
        List<Category> list = categoryMapper.selectByIdList(ids);
        if (CollectionUtils.isEmpty(list)){
            throw new XkException(ExceptionEnums.SPEC_PARAM_NOT_FOUND);
        }
        return list;
    }
}
